var tenYearStar='2008';
var plotDate = new Array();

// 模拟数据 start
var data1 = [[(new Date("2010/01/01")).getTime(), 5000],
		[(new Date("2021/01/01")).getTime(), 8000]];

var data2 = [[(new Date("2011/01/01")).getTime(), 500],
		[(new Date("2012/01/01")).getTime(), 1000]];

var data3 = [[0, 0],[(new Date("2011/2/01")).getTime(), 200],[(new Date("2011/2/06")).getTime(), 20],
		[(new Date("2011/2/20")).getTime(), 300]];
// 模拟数据 end

// 图表显示年的情况(10年 2010-2020)
function selectYear(data,startYear) {

	$.plot($("#placeholder"), [{ label: "总费用",  data: data}], {
				xaxis : {
					mode : "time",
					tickSize : [1, "year"],
					min : (new Date(startYear+"/01/01")).getTime(),
					max : (new Date((Number(startYear) + 10)+"/01/01")).getTime()
				}
			});
}
// 图表显示月的情况：查所选年份12个月
function selectMonth(data, yearData) {

	yearData = Ext.get('year').getValue();
	var afterYear = Number(yearData) + 1;

	$.plot($("#placeholder"), [{ label: "总费用",  data: data}], {
				xaxis : {
					mode : "time",
					tickSize : [1, "month"],
					min : (new Date(yearData + "/01/01")).getTime(),
					max : (new Date(afterYear + "/01/01")).getTime()
				}
			});
}
// 图表显示日的情况：查所选月份每日情况
function selectDay(data, yearData, monthData) {

	var afterMonth = Number(monthData) + 1;
	var afteryear = yearData;

	if (afterMonth == 13) {
		afterMonth = '01';
		afteryear = Number(yearData) + 1;
	}

	var ss=$.plot($("#placeholder"), [{ label: "总费用",  data: data}], {
				xaxis : {
					mode : "time",
					monthNames: '   ',
					tickSize : [1, "day"],
					min : (new Date(yearData + '/' + monthData + "/1")).getTime(),
					max : (new Date(afteryear + '/' + afterMonth + "/1")).getTime()
				},
				grid:{
					backgroundColor:{colors:['#fff','#d5e2f2']}
				}
	});
	ss.pointOffset({x:3,y:3.5}).left
//	alert(ss.offset().left);
//	var axes = plot.getAxes();
//	axes.xaxis.min.toFixed(2)
	
}

function selectflot() {

	var result='';
	var yearData = Ext.get('year').getValue();
	var monthData = Ext.get('month').getValue();

//	alert(yearData);
//	alert(monthData);
	
	// 年月都选0的情况
	if (yearData == 0 && monthData == 0) {

		var startYear = tenYearStar;

		searchTenYearsData(startYear);

		selectYear(plotDate,startYear);

		result = '2008年-2018年';

		// 年选0的情况 为：无法查询
	} else if (yearData == 0) {

		result = '';

		Ext.Msg.show({
					title : '无法查询',
					msg : '原因：只有月份，没有年份',
					animEl : 'elId',
					icon : Ext.MessageBox.INFO
				});

		// 月选0的情况
	} else if (monthData == 0) {

		searchYearData(yearData);

		selectMonth(plotDate, yearData);

		result = yearData + '年情况';

		// 都不选0的情况 为：查所选月份的每日情况
	} else {

		searchMonthData(yearData, monthData);

		selectDay(plotDate, yearData, monthData);
		result = yearData + '年' + monthData + '月情况';
	}

	Ext.get('beizhu').update(result);
	// $('#beizhu').html('查询结果为:2010年-2020年');
}

//数据库查询 start
// 查询当前月份的每日数据
function searchCurrentData(today) {
	
	plotDate=new Array();
	
	var sql = "where dc_year='" + today.getFullYear() + "' and dc_month='"
			+ (today.getMonth()+1) + "' order by dc_day desc";
	resultDate = dayCostDao.select(sql);

	for (var i = 0; i < resultDate.length; i++) {
		var result = resultDate[i];
		plotDate.push([
				(new Date(result.dc_year + '/' + result.dc_month + '/'
						+ result.dc_day)).getTime(), result.dc_cost]);
	}
}

// 查询指定月份的每日数据
function searchMonthData(yearData, monthData) {
	plotDate=new Array();
	
	var sql = "where dc_year='" + yearData + "' and dc_month='" + monthData
			+ "' order by dc_day desc";
	resultDate = dayCostDao.select(sql);
	
	for (var i = 0; i < resultDate.length; i++) {
		var result = resultDate[i];
		plotDate.push([
				(new Date(result.dc_year + '/' + result.dc_month + '/'
						+ result.dc_day)).getTime(), result.dc_cost]);
	}
}

// 查询指定年份的每月数据
function searchYearData(yearData) {
	
	plotDate=new Array();
	
	var sql = "where dc_year='" + yearData + "' order by dc_day desc";
	resultDate = dayCostDao.select(sql);

	var cost1 = cost2 = cost3 = cost4 = cost5 = cost6 = cost7 = cost8 = cost9 = cost10 = cost11 = cost12 = 0;

	for (var i = 0; i < resultDate.length; i++) {
		var result = resultDate[i];
		switch (result.dc_month) {
			case '01' :
				cost1 += Number(result.dc_cost);
				break;
			case '02' :
				cost2 += Number(result.dc_cost);
				break;
			case '03' :
				cost3 += Number(result.dc_cost);
				break;
			case '04' :
				cost4 += Number(result.dc_cost);
				break;
			case '05' :
				cost5 += Number(result.dc_cost);
				break;
			case '06' :
				cost6 += Number(result.dc_cost);
				break;
			case '07' :
				cost7 += Number(result.dc_cost);
				break;
			case '08' :
				cost8 += Number(result.dc_cost);
				break;
			case '09' :
				cost9 += Number(result.dc_cost);
				break;
			case '10' :
				cost10 += Number(result.dc_cost);
				break;
			case '11' :
				cost11 += Number(result.dc_cost);
				break;
			case '12' :
				cost12 += Number(result.dc_cost);
				break;
		}
	}
	
	plotDate.push([(new Date(yearData + '/01/01')).getTime(), cost1]);
	plotDate.push([(new Date(yearData + '/02/01')).getTime(), cost2]);
	plotDate.push([(new Date(yearData + '/03/01')).getTime(), cost3]);
	plotDate.push([(new Date(yearData + '/04/01')).getTime(), cost4]);
	plotDate.push([(new Date(yearData + '/05/01')).getTime(), cost5]);
	plotDate.push([(new Date(yearData + '/06/01')).getTime(), cost6]);
	plotDate.push([(new Date(yearData + '/07/01')).getTime(), cost7]);
	plotDate.push([(new Date(yearData + '/08/01')).getTime(), cost8]);
	plotDate.push([(new Date(yearData + '/09/01')).getTime(), cost9]);
	plotDate.push([(new Date(yearData + '/10/01')).getTime(), cost10]);
	plotDate.push([(new Date(yearData + '/11/01')).getTime(), cost11]);
	plotDate.push([(new Date(yearData + '/12/01')).getTime(), cost12]);
}
// 查询指定指定年份的10年数据
function searchTenYearsData(startYear) {
	
	plotDate=new Array();
	
	var sql = "where dc_year='" + startYear + "' or dc_year='"
			+ (Number(startYear) + 1) + "' or dc_year='"
			+ (Number(startYear) + 2) + "' or dc_year='"
			+ (Number(startYear) + 3) + "' or dc_year='"
			+ (Number(startYear) + 4) + "' or dc_year='"
			+ (Number(startYear) + 5) + "' or dc_year='"
			+ (Number(startYear) + 6) + "' or dc_year='"
			+ (Number(startYear) + 7) + "' or dc_year='"
			+ (Number(startYear) + 8) + "' or dc_year='"
			+ (Number(startYear) + 9) + "' order by dc_day desc";
			
	resultDate = dayCostDao.select(sql);
	
	var cost1 = cost2 = cost3 = cost4 = cost5 = cost6 = cost7 = cost8 = cost9 = cost10 =  0;

	for (var i = 0; i < resultDate.length; i++) {
		var result = resultDate[i];
		switch (result.dc_year) {
			case startYear :
				cost1 += Number(result.dc_year);
				break;
			case (Number(startYear) + 1) :
				cost2 += Number(result.dc_cost);
				break;
			case (Number(startYear) + 2) :
				cost3 += Number(result.dc_cost);
				break;
			case (Number(startYear) + 3) :
				cost4 += Number(result.dc_cost);
				break;
			case (Number(startYear) + 4) :
				cost5 += Number(result.dc_cost);
				break;
			case (Number(startYear) + 5) :
				cost6 += Number(result.dc_cost);
				break;
			case (Number(startYear) + 6) :
				cost7 += Number(result.dc_cost);
				break;
			case (Number(startYear) + 7) :
				cost8 += Number(result.dc_cost);
				break;
			case (Number(startYear) + 8) :
				cost9 += Number(result.dc_cost);
				break;
			case (Number(startYear) + 9) :
				cost10 += Number(result.dc_cost);
				break;			
		}
	}
	
	plotDate.push([(new Date(startYear + '/01/01')).getTime(), cost1]);
	plotDate.push([(new Date((Number(startYear) + 1) + '/01/01')).getTime(), cost2]);
	plotDate.push([(new Date((Number(startYear) + 2) + '/01/01')).getTime(), cost3]);
	plotDate.push([(new Date((Number(startYear) + 3) + '/01/01')).getTime(), cost4]);
	plotDate.push([(new Date((Number(startYear) + 4) + '/01/01')).getTime(), cost5]);
	plotDate.push([(new Date((Number(startYear) + 5) + '/01/01')).getTime(), cost6]);
	plotDate.push([(new Date((Number(startYear) + 6) + '/01/01')).getTime(), cost7]);
	plotDate.push([(new Date((Number(startYear) + 7)+ '/01/01')).getTime(), cost8]);
	plotDate.push([(new Date((Number(startYear) + 8) + '/01/01')).getTime(), cost9]);
	plotDate.push([(new Date((Number(startYear) + 9) + '/01/01')).getTime(), cost10]);
}
//数据库查询 end

Ext.onReady(function() {
	
	$('#flotSelect').click(function(){
		selectflot();
	});
	
	var today = new Date();

	var result = today.getFullYear() + '年' + (today.getMonth()+1) + '月';

	Ext.get('beizhu').update(result);

	searchCurrentData(today);
	selectDay(plotDate, today.getFullYear(), (today.getMonth()+1));
	
	// 图形部分使用(jquery flot)
//	selectDay();
//	searchYearData('2011');
});